

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Bean
 * @Description:
 * @Date: 2018-04-02 10:53
 */
public class MergeFileUtils {

    public static void main(String[] args)
    {

        while(true)
        {

        }
    }

    public static void t(String fileName)
    {
        File file = new File(fileName);
        System.out.println(file.getAbsoluteFile());

    }

    public static void start()
    {
        System.out.println("请选择合并方式(默认合并今天的日志):");
        System.out.println("1.合并所有日期的日志");
        System.out.println("2.合并指定日期的日志");
        System.out.println("3.合并今天的日志");

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        try{
            line = br.readLine().trim();
            File file = null;
            if("1".equals(line))
            {
                File[] files = new File("").listFiles();
                for(File f : files)
                {
                    if(f.isDirectory())
                    {
                        merge(f);
                    }
                }
            }
            else if("2".equals(line))
            {
                file = new File(line.trim());
                if(file.exists() && file.isDirectory())
                {
                    merge(file);
                }
                else
                {
                    System.out.println("你输入的目录不存在,请重新选择!");
                    start();
                }
            }
            else
            {
                String today = new SimpleDateFormat("yyyyMMdd").format(System.currentTimeMillis());
                file = new File(today);
                if(!file.exists())
                {
                    System.out.println("今天还没有日志记录!");
                }
                else
                {
                    merge(file);
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void merge(File directory)
    {
        if(directory == null || !directory.exists() || !directory.isDirectory())
        {
            System.out.println("目录错误!");
            return;
        }

        File[] files = directory.listFiles();
        StringBuilder result = new StringBuilder();
        List<String> list = null;
        for(File f : files)
        {
            list = readFile(f);
            if(list.size() == 0)
                continue;

            if(result.length() >= 0)
            {
                list.remove(list.get(0));
            }

            for(String s : list)
            {
                result.append(s);
                result.append("\n");
            }
        }

        writeFile(directory.getAbsolutePath() + ".txt", result.toString());
    }

    public static List<String> readFile(File file)
    {

        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader reader = null;

        List<String> result = new ArrayList<>();
        try {
            fis = new FileInputStream(file);
            isr = new InputStreamReader(fis, "utf-8");
            reader = new BufferedReader(isr);
            String e = "";

            while((e = reader.readLine()) != null) {
                if(e != null && e.length() > 0) {
                    result.add(e);
                }
            }
        } catch (Exception var23) {
            var23.printStackTrace();
        } finally {
            try {
                if(reader != null) {
                    reader.close();
                }
            } catch (IOException var22) {
                var22.printStackTrace();
            }

            try {
                if(isr != null) {
                    isr.close();
                }
            } catch (IOException var21) {
                var21.printStackTrace();
            }

            try {
                if(fis != null) {
                    fis.close();
                }
            } catch (IOException var20) {
                var20.printStackTrace();
            }

        }

        return result;
    }

    public static void writeFile(String fileName, String data)
    {
        FileOutputStream fos = null;
        OutputStreamWriter osw = null;
        BufferedWriter writer = null;

        try {
            fos = new FileOutputStream(new File(fileName));
            osw = new OutputStreamWriter(fos, "UTF-8");
            writer = new BufferedWriter(osw);
            writer.write(data);
            writer.flush();
        } catch (Exception var22) {
            var22.printStackTrace();
        } finally {
            try {
                if(writer != null) {
                    writer.close();
                }
            } catch (IOException var21) {
                var21.printStackTrace();
            }

            try {
                if(osw != null) {
                    osw.close();
                }
            } catch (IOException var20) {
                var20.printStackTrace();
            }

            try {
                if(fos != null) {
                    fos.close();
                }
            } catch (IOException var19) {
                var19.printStackTrace();
            }

        }

    }

}
